package com.xjgc.sap.ws.dao;

import com.xjgc.common.config.DbConn;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 屏柜与装置SAN号关联关系
 * @author Administrator
 *
 */
@Component
public class ZxjEip2Cpzsninfo {
	public String update(DbConn db){
		
		String sql = "select pgbh,zzbh from zxj_eip_2cpzsn_info where pg_productionorderno ISNULL  or pg_productionorderno=''";
		String updateSql = "update zxj_eip_2cpzsn_info set pg_productionorderno = ? where zzbh=?";
		
		PreparedStatement pst = null;
		PreparedStatement updatePst = null;
		ResultSet rs = null;
		String result = null;
		
		try {
			pst = db.getConnetion().prepareStatement(sql);
			rs = pst.executeQuery();
			updatePst = db.getConnetion().prepareStatement(updateSql);
			while(rs.next()) {
				updatePst.setString(1,getNo(rs.getString("pgbh"),db));
				updatePst.setString(2, rs.getString("zzbh"));
				updatePst.execute();
			}
			updatePst.close();
			
			
			rs.close();
			pst.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.closeCn();
		}

		return result ;
	}
	
	//pgbh  拆分去生产订单表中查询，找到 生产订单号
	public String getNo(String pgbh,DbConn db) {
		String pg_productionorderno="X";
		if(pgbh.contains("-")) {
			int l = pgbh.indexOf("-");
			int z = pgbh.length();
			String sql = "select no from zxj_eip_2caufnr where salesorderno ='"+pgbh.substring(0, l)+"' and salesorderlineprojectsno='"+pgbh.substring(l+1, z)+"'";
			PreparedStatement pst1 = null;
			ResultSet rs1 = null;
			
			try {
				pst1 = db.getConnetion().prepareStatement(sql);
				rs1 = pst1.executeQuery();
				while(rs1.next()) {
					pg_productionorderno = rs1.getString("no");
				}
				rs1.close();
				pst1.close();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				db.closeCn();
			}
		}
		
		return pg_productionorderno;
	}
}
